<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="78px"
    >
      <el-form-item label="处理人员" prop="solveWorker">
        <el-input
          v-model="queryParams.solveWorker"
          placeholder="请输入处理人员"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="告警标题" prop="solveWorker">
        <el-input
          v-model="queryParams.alarmValue"
          placeholder="请输入告警标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="告警时间" prop="happenTime">
        <el-date-picker
          v-model="happenTime"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="preserveList"
      border
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="告警标题" align="center" prop="alarmValue" />
      <!-- <el-table-column label="告警内容" align="center" prop="alarmContent" /> -->
      <el-table-column
        label="告警发生时间"
        align="center"
        prop="happenTime"
        width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ scope.row.happenTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理时长" align="center" prop="solveTime" />
      <el-table-column
        label="告警消除时间"
        align="center"
        prop="erasureTime"
        width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span>{{ scope.row.erasureTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处理人员" align="center" prop="solveWorker" />
      <el-table-column label="发生环节" align="center" prop="happenProcess" />
      <el-table-column label="告警级别" align="center" prop="alarmLevel">
        <template slot-scope="scope">
          {{ scope.row.alarmLevel | statusXX }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="state">
        <template slot-scope="scope">
          <el-tag :type="scope.row.state * 1 == 1 ? 'primary' : 'warning'">
            {{ scope.row.state * 1 == 1 ? "已处理" : "未处理" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['assembly:preserve:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleClear(scope.row.id)"
            v-hasPermi="['assembly:preserve:edit']"
            >清除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改告警维护记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="告警内容" prop="alarmContent">
          <el-input v-model="form.alarmContent" placeholder="请输入告警内容" />
        </el-form-item>
        <el-form-item label="告警原因" prop="alarmReason">
          <el-input v-model="form.alarmReason" placeholder="请输入告警原因" />
        </el-form-item>
        <el-form-item label="处理方案" prop="solveScheme">
          <el-input v-model="form.solveScheme" placeholder="请输入处理方案" />
        </el-form-item>
        <el-form-item label="处理人员" prop="solveWorker">
          <el-input v-model="form.solveWorker" placeholder="请输入处理人员" />
        </el-form-item>
        <el-form-item label="发生环节" prop="happenProcess">
          <el-input v-model="form.happenProcess" placeholder="请输入发生环节" />
        </el-form-item>
        <el-form-item label="告警级别" prop="happenProcess">
          <!-- <el-input v-model="form.alarmLevel" placeholder="请输入发生环节" /> -->
          <el-select v-model="form.alarmLevel" placeholder="请选择">
            <el-option label="紧急告警" :value="1" />
            <el-option label="重要告警" :value="2" />
            <el-option label="次要告警" :value="3" />
            <el-option label="一般告警" :value="4" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listPreserve,
  getPreserve,
  delPreserve,
  addPreserve,
  updatePreserve,
  clearAlarm,
} from "@/api/assembly/preserve";

export default {
  name: "Preserve",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 告警维护记录表格数据
      preserveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        alarmContent: null,
        happenTime: null,
        solveTime: null,
        erasureTime: null,
        alarmReason: null,
        solveScheme: null,
        solveWorker: null,
        happenProcess: null,
        alarmLevel: null,
        state: null,
        startTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      happenTime: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询告警维护记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.startTime = this.happenTime[0];
      this.queryParams.endTime = this.happenTime[1];
      listPreserve(this.queryParams).then((response) => {
        this.preserveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        alarmContent: null,
        happenTime: null,
        solveTime: null,
        erasureTime: null,
        alarmReason: null,
        solveScheme: null,
        solveWorker: null,
        happenProcess: null,
        alarmLevel: null,
        state: null,
        remark: null,
        startTime: null,
        endTime: null,
      };
      this.happenTime = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.happenTime = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加告警维护记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getPreserve(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改告警维护记录";
      });
    },
    handleClear(id) {
      this.$confirm("此操作清除该报警信号, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        clearAlarm(id).then((res) => {
          this.$message.success("操作成功");
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updatePreserve(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPreserve(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除告警维护记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delPreserve(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "assembly/preserve/export",
        {
          ...this.queryParams,
        },
        `preserve_${new Date().getTime()}.xlsx`
      );
    },
  },
  filters: {
    statusXX(arg) {
      let arr = ["紧急告警", "重要告警", "次要告警", "一般告警"];
      return arr[arg - 1];
    },
  },
};
</script>
